#include "Poco/Process.h"
#include "Process.h"

using namespace SystemTool;

// Get current process id
int Process::GetCurProcessId()
{
    return Poco::Process::id();
}

// Get current process excuted time in system's user mode kernal
long Process::GetCurProcessUserModeConsumeTime()
{
    long lUserModeTime = 0;
    long lKernalModeTime = 0;
    Poco::Process::times(lUserModeTime,lKernalModeTime);
    return lUserModeTime;
}

// Get current process excuted time in system's user mode kernal
long Process::GetCurProcessKernalModeConsumeTime()
{
    long lUserModeTime = 0;
    long lKernalModeTime = 0;
    Poco::Process::times(lUserModeTime,lKernalModeTime);
    return lKernalModeTime;
}

// Kill a process
void Process::Kill(int iPid)
{
    Poco::Process::kill(iPid);
}

// Is process running
bool Process::IsRunning(int iPid)
{
    if(Poco::Process::isRunning(iPid))
    {
        return true;
    }
    return false;
}

// Launch the process
int Process::Launch(std::string strExeFilePath,
                    std::vector<std::string> &vArgTable,
                    int &iPid,
                    std::string strInitialDirectory,
                    bool bIsWaitForExit)
{
    if(strExeFilePath.empty())
    {
        return -1;
    }

    Poco::ProcessHandle hProcess = Poco::Process::launch(strExeFilePath, vArgTable, strInitialDirectory, 0);
    iPid = hProcess.id();
    
    if(bIsWaitForExit)
    {
        return hProcess.wait();
    }

    return 0;
}
